package com.wlyuan.open.domain.source.dispatcher.conversion;

import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.dto.OpenContractSyncDTO;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OpenDispatcherContractConversion implements OpenDispatcherConversion<OpenContractSyncDTO> {
    private final BaseOpenDispatcherConversion baseOpenDispatcherConversion;

    @Override
    public void convert(OpenContractSyncDTO source) {
        convertInfo(source);
        //甲方
        convertFirst(source);
        //乙方
        convertSecond(source);
    }

    private void convertInfo(OpenContractSyncDTO source) {
        var tenant = source.getTenantId();
        if (LongUtils.notNullLong(tenant)) {
            var tenantId = baseOpenDispatcherConversion.tenant(tenant);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenContractSyncDTO租户不存在:" + tenant), source);
            source.setTenantId(tenantId);
        }
        //创建人
        if (LongUtils.notNullLong(source.getCreateBy())) {
            var createId = baseOpenDispatcherConversion.user(source.getCreateBy());
            AssertUtils.assertNotNull(createId, new ValueError("OpenContractSyncDTO创建人不存在:" + source.getCreateBy()), source);
            source.setCreateBy(createId);
        }
    }

    private void convertFirst(OpenContractSyncDTO source) {
        var first = source.getFirst();
        if (Optional.ofNullable(first).isPresent()) {
            if (LongUtils.notNullLong(first.getTenantId())) {
                var tenantId = baseOpenDispatcherConversion.tenant(first.getTenantId());
                AssertUtils.assertNotNull(tenantId, new ValueError("OpenContractSyncDTO甲方租户不存在:" + first.getTenantId()), source);
                first.setTenantId(tenantId);
            }
            if (LongUtils.notNullLong(first.getConfirm())) {
                var createId = baseOpenDispatcherConversion.user(first.getConfirm());
                AssertUtils.assertNotNull(createId, new ValueError("OpenContractSyncDTO甲方审核人不存在:" + first.getConfirm()), source);
                first.setConfirm(createId);
            }
        }
    }

    private void convertSecond(OpenContractSyncDTO source) {
        var second = source.getSecond();
        if (Optional.ofNullable(second).isPresent()) {
            if (LongUtils.notNullLong(second.getTenantId())) {
                var tenantId = baseOpenDispatcherConversion.tenant(second.getTenantId());
                AssertUtils.assertNotNull(tenantId, new ValueError("OpenContractSyncDTO乙方租户不存在:" + second.getTenantId()), source);
                second.setTenantId(tenantId);
            }
            if (LongUtils.notNullLong(second.getConfirm())) {
                var createId = baseOpenDispatcherConversion.user(second.getConfirm());
                AssertUtils.assertNotNull(createId, new ValueError("OpenContractSyncDTO乙方审核人不存在:" + second.getConfirm()), source);
                second.setConfirm(createId);
            }
        }
    }
}
